#!/usr/bin/env bash

# Copyright 2018-2020 Datawire. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License

set -o errexit
set -o nounset

# Don't bother trying to build the website if GH_TOKEN is unset (as it will
# be for a community PR).
if [ -z "${GH_TOKEN:-}" ]; then
	echo "[${0##*/}] skipping: GH_TOKEN is not set"
	exit 0
fi

set -o verbose

amb_dir=$PWD # ~/project/ambassador
git_workdir=$(git rev-parse --show-toplevel) # either ~/project (for apro.git) or ~/project/ambassador (for ambassador.git)

rm -rf "/tmp/getambassador.io"
git clone --single-branch https://d6e-automaton:${GH_TOKEN}@github.com/datawire/getambassador.io.git "/tmp/getambassador.io"
cd "/tmp/getambassador.io"

git submodule update --init --reference="$git_workdir"
# Swap out the "pre-release" info for our stuff.  We do this with a dumb
# `rm`/`cp` rather than a simple checkout
#     $ git -C submodules/pre-release checkout "$CIRCLE_SHA1"
# because we might be in apro.git instead of ambassador.git, and I
# don't want to deal with having to do a subtree split here.
for submodule in pre-release; do
	rm -rf submodules/"$submodule"
	mkdir submodules/"$submodule"
	cp -prv $amb_dir/docs submodules/"$submodule"/docs
done

yarn install
yarn run build
